package com.lvcoding.spring5.annotation3x.config;

import com.lvcoding.spring5.annotation3x.bean.ConnectionBeanFactory;
import com.lvcoding.spring5.annotation3x.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @Description 描述
 * @Date 2020-07-22 3:13 下午
 * @Author wuyanshen
 */
@Configuration
public class AppConfig {

    @Bean("user")
    @Scope("prototype")
    public User user(){
        return new User();
    }

    @Bean("conn")
    public Connection connection(){
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mbtest?useSSL=false","root", "root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    @Bean
    public Connection connection2(){
        try {
            return new ConnectionBeanFactory().getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
